package com.colorful.insuranceproject.adapter;

import android.content.Context;
import android.support.annotation.NonNull;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.colorful.insuranceproject.R;
import com.colorful.insuranceproject.bean.HealthInfoBean;
import com.lovely3x.common.adapter.BaseViewHolder;
import com.lovely3x.common.adapter.ListAdapter;
import com.lovely3x.common.widgets.EnoughHeightGridView;
import com.lovely3x.imageloader.ImageLoader;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;

/**
 * Created by handsome-Bob on 2017/10/27.
 * <p>
 * 首页的健康资讯的适配器
 */
public class HomeHealthInfoAdapter extends ListAdapter<HealthInfoBean> {

    private GridImgsAdapter.ImageClickedListener mGridImageViewClickLitener;

    public HomeHealthInfoAdapter(List<HealthInfoBean> datas, Context context) {
        super(datas, context);
    }

    @NonNull
    @Override
    public BaseViewHolder createViewHolder(int position, ViewGroup parent) {
        switch (getItemViewType(position)) {
            case 1:
                return new ViewHolderSingle(getLayoutInflater().inflate(R.layout.item_health_info, parent, false));
            case 3:
                return new ViewHolderMore(getLayoutInflater().inflate(R.layout.item_health_info_more_pic, parent, false));
        }
        throw new IllegalStateException("unknown type");
    }

    @Override
    public int getItemViewType(int position) {
        HealthInfoBean item = getDatas().get(position);
        int size;
        if (item.getImages().size() < 3) {
            size = 1;
        } else {
            size = 3;
        }
        return size;
    }

    @Override
    public void handleData(int position, @NonNull BaseViewHolder holder) {
        switch (holder.getType()) {
            case 1:
                loadDataSingle(position, (ViewHolderSingle) holder);
                break;
            case 3:
                loadDataMore(position, (ViewHolderMore) holder);
                break;
        }
    }

    /**
     * 多张图片
     */
    private void loadDataMore(int position, ViewHolderMore holder) {
        HealthInfoBean bean = datas.get(position);
        holder.tvTitle.setText(bean.getTitle());
        holder.tvFrom.setText(bean.getAuthor());
        List<String> list = new ArrayList<>();
        if (bean.getImages().size() >= 3) {
            for (int i = 0; i < 3; i++) {
                list.add(bean.getImages().get(i));
            }
        } else {
            list = bean.getImages();
        }

        CommentImgsAdapter imgsAdapter = new CommentImgsAdapter(list, position, mContext, holder.moreImg);
        holder.moreImg.setFocusable(false);
        holder.moreImg.setFocusableInTouchMode(false);
        holder.moreImg.setAdapter(imgsAdapter);
        imgsAdapter.setOnImgViewClickedListener(mGridImageViewClickLitener);
    }

    /**
     * 单张图片
     */
    private void loadDataSingle(int position, ViewHolderSingle holder) {
        HealthInfoBean bean = datas.get(position);
        holder.tvTitle.setText(bean.getTitle());
        holder.tvFrom.setText(bean.getAuthor());
        if (bean.getImages() != null && !bean.getImages().isEmpty()) {
            ImageLoader.getInstance().display(holder.singleImg, bean.getImages().get(0));
        } else {
            holder.singleImg.setImageResource(R.drawable.load_pic);
        }
    }

    public static class ViewHolderSingle extends BaseViewHolder {

        @Bind(R.id.tv_title)
        TextView tvTitle;

        @Bind(R.id.tv_from)
        TextView tvFrom;

        @Bind(R.id.img_single_pic)
        ImageView singleImg;

        public ViewHolderSingle(View rootView) {
            super(rootView);
        }

        @Override
        public int getType() {
            return 1;
        }
    }

    public static class ViewHolderMore extends BaseViewHolder {

        @Bind(R.id.tv_title)
        TextView tvTitle;

        @Bind(R.id.tv_from)
        TextView tvFrom;

        @Bind(R.id.ehgv_pic)
        EnoughHeightGridView moreImg;

        public ViewHolderMore(View rootView) {
            super(rootView);
        }

        @Override
        public int getType() {
            return 3;
        }
    }

    public static class CommentImgsAdapter extends GridImgsAdapter {

        public CommentImgsAdapter(List<String> datas, int parentPosition, Context context, EnoughHeightGridView gridView) {
            super(datas, parentPosition, context, gridView);
        }
    }

    /**
     * 设置图片点击监听器
     *
     * @param listener 需要设置的图片点击监听器
     */
    public void setOnImgViewClickedListener(GridImgsAdapter.ImageClickedListener listener) {
        this.mGridImageViewClickLitener = listener;
    }
}
